Skip to content

Use slices and cmp packages instead of sort#4951

Merged
pietern merged 1 commit intomainfrom
go1.21-slices-package
Apr 14, 2026
Merged

Use slices and cmp packages instead of sort#4951
pietern merged 1 commit intomainfrom
go1.21-slices-package

Conversation

@pietern
Copy link
Copy Markdown
Contributor

@pietern pietern commented Apr 13, 2026

Summary

Migrate from the sort package to the slices and cmp packages across the codebase.

The slices package (stable since Go 1.21, extended in Go 1.23) provides type-safe, generic alternatives to sort. Go 1.23 added slices.Sorted and slices.SortedFunc, making the full migration more compelling since the sort import can often be dropped entirely.

Key replacements:

  • sort.Stringsslices.Sort
  • sort.Sliceslices.SortFunc
  • sort.SliceStableslices.SortStableFunc

The original prompt identified ~25 sort.Strings call sites and ~3 sort.SliceStable call sites across ~46 files importing sort. This is a low-risk, mechanical migration.

The .golangci.yaml configuration is updated to flag any new usage of the sort package.

Test plan

  • Existing unit tests pass (make test)
  • Linter passes (make lint)

This pull request was AI-assisted by Isaac.

Migrate all `sort.Slice`, `sort.SliceStable`, and `sort.Strings` calls
to their standard library equivalents introduced in Go 1.21:
`slices.SortFunc`, `slices.SortStableFunc`, and `slices.Sort`.

Add `forbidigo` lint rules to prevent reintroduction.

See https://go.dev/doc/go1.21#slices

Co-authored-by: Isaac
@pietern pietern added this pull request to the merge queue Apr 14, 2026
Merged via the queue into main with commit 7d4a13a Apr 14, 2026
27 checks passed
@pietern pietern deleted the go1.21-slices-package branch April 14, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants